package cn.imvc.app.models;

import java.util.Collection;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.web.util.matcher.RequestMatcher;
import org.springframework.stereotype.Service;

import cn.imvc.app.entities.Article;
import cn.imvc.app.entities.ArticleClass;
import cn.imvc.app.entities.Resource;
import cn.imvc.app.repositories.ArticleClassRepository;
import cn.imvc.app.repositories.ArticleRepository;
import cn.imvc.core.ICache;
import cn.imvc.core.ModelUtil;
import cn.imvc.data.domain.ViewRequest;

@Service
public class ArticleModelImpl implements ArticleModel {

	@Autowired
	ArticleRepository articleRepository;
	@Autowired
	ArticleClassRepository articleClassRepository;
	@Autowired
	RefRelationModel refRelationModel;
	@Autowired
	ICache cache;
	
	public LinkedHashMap<RequestMatcher, Collection<ConfigAttribute>> getRequestMap() {
		return null;
	}

	public Page<Article> find(ViewRequest viewRequest) {
		return articleRepository.findAll(ModelUtil.OrderByIdDesc(viewRequest));
	}

	public Article get(int id) {
		return articleRepository.findOne(id);
	}

	public void delete(int id) {
		
	   articleRepository.delete(id);
	}

	public void deleteBatch(int[] ids) {
		for (int id : ids) {
			delete(id);
		}
	}

	public Article save(Article article,int classId) {
		article.setTime(new Date());
		ArticleClass articleClass = articleClassRepository.findOne(classId);
		article.setArticleClass(articleClass);
		Article articleTmp = articleRepository.saveAndFlush(article);
		cache.removeSecond("MAX_ID", "article");
		return articleTmp;
	}

	public Article update(int id, Article article,int classId) {
		if (classId != 0 && article.getTime() != null) {
			article.setTime(new Date());
			article.setArticleClass(articleClassRepository.getOne(classId));
			Article loadResource = get(id);
			BeanUtils.copyProperties(article, loadResource, new String[]{"id"});
			return articleRepository.saveAndFlush(loadResource);
		} else if(classId != 0){
			article.setTime(new Date());
			article.setArticleClass(articleClassRepository.getOne(classId));
			Article loadResource = get(id);
			BeanUtils.copyProperties(article, loadResource, "id");
			return articleRepository.saveAndFlush(loadResource);
		} else if(article.getTime() != null){
			article.setTime(new Date());
			Article loadResource = get(id);
			BeanUtils.copyProperties(article, loadResource, "id","articleClass");
			return articleRepository.saveAndFlush(loadResource);
		} else{
			article.setTime(new Date());
			Article loadResource = get(id);
			BeanUtils.copyProperties(article, loadResource, "id","articleClass");
			return articleRepository.saveAndFlush(loadResource);
		}
	}

	public Article merge(Article article) {
		return articleRepository.saveAndFlush(article);
	}

}
